package jin.competition.jan20co12;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class BankUserControl {
	// 银行账户注册 （身份证号码，姓名） （1）用户都有一个自己的资金账户
	public boolean accountRegister(BankUser bankuser) {
		boolean result = false;
		BaseDao dao = new BaseDao();
		Connection con = dao.getCon();
		String sql = "insert into bankuser (id,name,pwd,moneyinaccount) values (?,?,?,?)";

		try {
			PreparedStatement ps = con.prepareStatement(sql);
			ps.setString(1, bankuser.getId());
			ps.setString(2, bankuser.getName());
			ps.setString(3, bankuser.getPwd());
			ps.setDouble(4, bankuser.getCash());
			result = ps.execute();

			if (ps != null) {
				ps.close();
			}

			if (con != null) {
				con.close();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return result;

	}

	// （2）用户可以存钱到账户 (accountid,money)
	public boolean bankMoney(int accountid, double money) {
		boolean result = false;
		BaseDao dao = new BaseDao();
		Connection con = dao.getCon();
		String sql1 = "select moneyinaccount from bankuser where accountid="+accountid+"";
		String sql2 = "update bankuser set moneyinaccount=? where accountid="+accountid+"";
		
		try {
			double account = 0;
			PreparedStatement ps1 = con.prepareStatement(sql1);
			ResultSet rs = ps1.executeQuery();
			while (rs.next()) {
				account = rs.getDouble("moneyinaccount");
			}
			PreparedStatement ps2 = con.prepareStatement(sql2);
			ps2.setDouble(1, (money + account));
			System.out.println("个人账号:"+accountid+"  该账户现在有"+(money + account)+" 元");
			//ps2.setInt(2, banku.getAccountId());
			result=ps2.execute();

			if (rs != null) {
				rs.close();
			}
			if (ps1 != null) {
				ps1.close();
			}
			if (ps2 != null) {
				ps2.close();
			}
			if (con != null) {
				con.close();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return result;
	}
	
	//（3）用户可以转账到其他人的账户    (用户accountid，收款方accountid,钱)
	public boolean transfer(BankUser bankuser,int accountid, double money) {
		boolean result = false;
		BaseDao dao = new BaseDao();
		Connection con = dao.getCon();
		String sqla="select * from bankuser where accountid="+bankuser.getAccountId()+"";
		String sqlb="update bankuser set moneyinaccount=? where accountid="+bankuser.getMoneyInAccount()+"";
		String sql1 = "select moneyinaccount from bankuser where accountid="+accountid+"";
		String sql2 = "update bankuser set moneyinaccount=? where accountid="+accountid+"";
		
		try {
			double myAccount = 0;
			double otherSideAccount = 0;
			PreparedStatement psa = con.prepareStatement(sqla);
			ResultSet rsa = psa.executeQuery();
			while (rsa.next()) {
				myAccount = rsa.getDouble("moneyinaccount");
			}

			PreparedStatement psb = con.prepareStatement(sqlb);
			psb.setDouble(1, (myAccount-money));
			//========================================
			PreparedStatement ps1 = con.prepareStatement(sql1);
			ResultSet rs1 = psa.executeQuery();
			while (rs1.next()) {
				otherSideAccount = rs1.getDouble("moneyinaccount");
			}
			PreparedStatement ps2 = con.prepareStatement(sql2);
			ps2.setDouble(1, (money +otherSideAccount));
			
			System.out.println("--转账成功--");
			System.out.println("您的账户本来有 "+myAccount+" 元。"+"刚刚转账 "+money+" 元给"+accountid);
			System.out.println("您的账户号:"+bankuser.getAccountId()+"  该账户现在有"+(myAccount-money)+" 元");
			//ps2.setInt(2, banku.getAccountId());
			result=ps2.execute();
			if (rsa != null) {
				rsa.close();
			}

			if (rs1 != null) {
				rs1.close();
			}
			
			if (psa != null) {
				psa.close();
			}
			if (psb != null) {
				psb.close();
			}
			if (ps1 != null) {
				ps1.close();
			}
			
			if (ps2 != null) {
				ps2.close();
			}
			if (con != null) {
				con.close();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return result;
	}
	//（4）用户可以注销自己的资金账户
	public boolean deactivate(String id,String name,String pwd,int accountId){
		boolean result = false;
		BaseDao dao = new BaseDao();
		Connection con = dao.getCon();
		String sql="delete from bankuser where id='"+id+"'and name='"+name+"'and pwd='"+pwd+"' and accountid="+accountId+"";
		try {
			PreparedStatement ps=con.prepareStatement(sql);
		
			result=ps.execute();
			if(result){
				System.out.println("您的账户："+accountId+"已经注销");
			}
			if(ps!=null){
	    		ps.close();    		   		
	    	  }
	    	  if(con!=null){
	    		con.close();    		   		
	    	  }		
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return result;
	}
	
	
	
	

}

/*
 * 实现银行的部分逻辑 
 * （1）用户都有一个自己的资金账户 
 * （2）用户可以存钱到账户 
 * （3）用户可以转账到其他人的账户 
 * （4）用户可以注销自己的资金账户
 */